package com.sws.hm.controller;


import com.sws.common.BaseResponse;
import com.sws.common.StatusCode;
import com.sws.hm.dao.portDao;
import com.sws.hm.dto.port;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@RestController
@RequestMapping("/port")
@CrossOrigin
@Slf4j
public class PortController {
    @Autowired
    private portDao dao;

    @PostMapping("add")
    public BaseResponse add(String mc, String ip, String port, int ifsocket) {
        UUID uuid= UUID.randomUUID();
        String uuidString=uuid.toString();
        dao.insertSocket(uuidString,mc,ip,port,"2",ifsocket,0);
        BaseResponse response=new BaseResponse(StatusCode.Success);
        return response;
    }

    @RequestMapping("findAll")
    public  BaseResponse findAll(String mc) {
        List<port> data=dao.findAll(mc);
        int count=data.size();
        BaseResponse response=new BaseResponse(StatusCode.Success);
        response.setCount(count);
        response.setData(data);
        return response;
    }




    @PostMapping("deleteById")
    public BaseResponse delete(String id) {

        int num=dao.deleteById(id);
        BaseResponse response=new BaseResponse(StatusCode.Success);
        response.setCount(num);
        return response;
    }



    @RequestMapping("UpdatePort")
    public BaseResponse UpdatePort(HttpServletRequest request) {
        String id=request.getParameter("id");
        String mc=request.getParameter("mc");
        String ip=request.getParameter("ip");
        String port=request.getParameter("port");
        String ifsocket=request.getParameter("ifsocket");

        int intifsocket=Integer.parseInt(ifsocket);
        int num=dao.updatePort(id,mc,ip,port,intifsocket);

        BaseResponse response=new BaseResponse(StatusCode.Success);
        response.setCount(num);
        return response;
    }

}
